package demo.msa.zookeeper;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * Created by admin on 2017/9/14.
 */
public class ZooKeeperDemo {
    private static final String CONNECTION_STRING = "101.251.209.225:2181";
    private static final int SESSION_TIMEOUT = 5000;

    private static CountDownLatch latch = new CountDownLatch(1);

//    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
//        //连接ZooKeeper
//        ZooKeeper zk =new ZooKeeper(CONNECTION_STRING, SESSION_TIMEOUT, new Watcher() {
//            @Override
//            public void process(WatchedEvent watchedEvent) {
//                if(watchedEvent.getState() == Event.KeeperState.SyncConnected){
//                    latch.countDown();
//                }
//            }
//        });
//        latch.await();
//        //获取ZooKeeper客户端对象
//        System.out.println("ZooKeeper对象信息："+zk);
//        //以同步方式获取子节点
//        System.out.println("列出以同步方式获取的子节点：");
//        List<String> children = zk.getChildren("/",null);
//        for(String node : children){
//            System.out.println(node);
//        }
//        //以异步方式获取子节点
////        System.out.println("列出以异步方式获取的子节点：");
////        zk.getChildren("/",null,new AsyncCallback.ChildrenCallback(){
////            @Override
////            public void processResult(int rc, String path, Object ctx, List<String> children) {
////                for(String node : children){
////                    System.out.println(node);
////                }
////            }
////        },null);
////        Thread.sleep(Long.MAX_VALUE);
//        //以同步方式判断节点是否已存在
//        Stat stat = zk.exists("/",null);
//        if(stat != null){
//            System.out.println("node exists");
//        }else{
//            System.out.println("node does not exists");
//        }
//        //以异步方式判断节点是否已存在
////        zk.exists("/", null, new AsyncCallback.StatCallback() {
////            @Override
////            public void processResult(int i, String s, Object o, Stat stat) {
////                if(stat != null){
////                    System.out.println("node exists");
////                }else{
////                    System.out.println("node does not exists");
////                }
////            }
////        }, null);
//
//        if(zk.exists("/foo",null) == null){
//            //以同步方式创建一个持久节点
//            String name = zk.create("/foo","hello".getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
//            System.out.println(name);
//        }
//        //以同步方式获取/foo节点的数据
//        byte[] data = zk.getData("/foo", null, null);
//        System.out.println(new String(data));
//        //以同步方式更新/foo节点的数据
//        Stat stat1 = zk.setData("/foo", "hi".getBytes(), -1);
//        System.out.println(stat1 != null);
//        //以同步方式删除/foo节点
//        zk.delete("/foo",-1);
//        System.out.println(true);
//    }
}
